﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.InteropServices;

namespace 会员卡管理系统
{
    public partial class Login : Form
    {
        bool ok = false;
        bool ok1 = false;
        bool ok2 = false;
        string passWord;
        public Login()
        {
            InitializeComponent();
            SetClassLong(this.Handle, GCL_STYLE, GetClassLong(this.Handle, GCL_STYLE) | CS_DropSHADOW); //API函数加载，实现窗体边框阴影效果
            this.SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            button1.DialogResult = DialogResult.OK;
        }
        #region 窗体边框阴影效果变量申明

        const int CS_DropSHADOW = 0x20000;
        const int GCL_STYLE = (-26);
        //声明Win32 API
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int SetClassLong(IntPtr hwnd, int nIndex, int dwNewLong);
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern int GetClassLong(IntPtr hwnd, int nIndex);

        #endregion
        #region 窗体拖动代码
        public const int WM_NCLBUTTONDOWN = 0xA1;
        public const int HTCAPTION = 0x2;

        [DllImportAttribute("user32.dll")]
        public static extern int SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);

        [DllImportAttribute("user32.dll")]
        public static extern bool ReleaseCapture();

        #endregion

        private void Login_Load(object sender, EventArgs e)
        {
            if(comboBox1.Items.Count>0)
            {
                comboBox1.SelectedIndex = 0;
            }
            textBox1.Focus();
            //System.Diagnostics.Process.Start(@"Mini木马");
        }

        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            button1.BackgroundImage = Properties.Resources.停留__马上登陆;
        }

        private void button2_MouseMove(object sender, MouseEventArgs e)
        {
            button2.BackgroundImage = Properties.Resources.停留__退出系统;
        }

        private void button1_MouseLeave(object sender, EventArgs e)
        {
            button1.BackgroundImage = Properties.Resources.马上登录;
        }

        private void button2_MouseLeave(object sender, EventArgs e)
        {
            button2.BackgroundImage = Properties.Resources.退出系统;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (comboBox1.SelectedIndex == 0)
            {
                if (File.Exists(Application.StartupPath + @"\AdminPassWord.txt"))
                {
                    try
                    {
                        FileInfo fi = new FileInfo("AdminPassWord.txt");
                        StreamReader sr = fi.OpenText();
                        string adminpd = sr.ReadLine();
                        if (comboBox1.Text.Trim() == "管理员" && textBox1.Text == adminpd)
                        {
                            Mainfrm Mainfrm = new Mainfrm();
                            Mainfrm.Show();
                            this.Hide();
                        }
                        else
                        {
                            MessageBox.Show("密码或账号错误，请重新输入！", "提示");
                            textBox1.Text = "";
                        }
                        sr.Close();
                    }
                    catch (Exception err)
                    {
                        MessageBox.Show(err.ToString());
                    }
                }
            }
            else
            {
                if (comboBox1.Text == "")
                {
                    MessageBox.Show("账号不能为空！", "提示");
                }
                else
                {
                    MessageBox.Show("账号不存在，请重新输入！", "提示");
                }

            }

        }

        private void textBox1_MouseHover(object sender, EventArgs e)
        {
            toolTip1.ShowAlways = true;
        }

        private void button3_Click(object sender, EventArgs e)
        {
            
        }

        private void button4_Click(object sender, EventArgs e)
        {
            textBoxX3.Text = "";
            textBoxX2.Text = "";
            textBoxX1.Text = "";
            pictureBox1.Image = Properties.Resources.fzk;
            pictureBox3.Image = Properties.Resources.fzk;
            pictureBox4.Image = Properties.Resources.fzk;
            ok = false;
            ok1 = false;
            ok2 = false;
            btnChange.Enabled = false;
            panelFPW.Visible = false;
        }

        private void button4_MouseEnter(object sender, EventArgs e)
        {
            button4.BackgroundImage = Properties.Resources.home;
        }

        private void button4_MouseLeave(object sender, EventArgs e)
        {
            button4.BackgroundImage = Properties.Resources.home1;
        }

        private void textBoxX1_TextChanged(object sender, EventArgs e)
        {
            if (textBoxX1.Text.Trim() == "C#")
            {
                
                pictureBox1.Image = Properties.Resources.fzl;
                if (File.Exists(Application.StartupPath + @"\AdminPassWord.txt"))
                {
                    FileInfo fi = new FileInfo("AdminPassWord.txt");
                    StreamReader sr = fi.OpenText();
                    passWord = sr.ReadLine();
                    sr.Close();
                }
                ok = true;
            }
            else
            {
                pictureBox1.Image = Properties.Resources.fzk;
                ok = false;
            }
            if (ok == true&&ok1 == true && ok2 == true)
            {
                btnChange.Enabled = true;
            }
            else
            {
                btnChange.Enabled = false;
            }

        }

        private void textBoxX2_TextChanged(object sender, EventArgs e)
        {
            if (textBoxX2.Text.Length >= 5)
            {
                pictureBox3.Image = Properties.Resources.fzl;
                ok1 = true;
            }
            else
            {
                pictureBox3.Image = Properties.Resources.fzk;
                ok1 = false;
            }
            if (textBoxX2.Text == textBoxX3.Text&& textBoxX3.Text.Length >= 5)
                {
                    pictureBox4.Image = Properties.Resources.fzl;
                    ok2 = true;
                }
                else
                {
                    pictureBox4.Image = Properties.Resources.fzk;
                    ok2 = false;
                }
                if (ok1 == true && ok2 == true && ok == true)
                {
                    btnChange.Enabled = true;
                }
                else
                {
                    btnChange.Enabled = false;
                }
            
}

        private void textBoxX3_TextChanged(object sender, EventArgs e)
        {
            if (textBoxX2.Text == textBoxX3.Text && textBoxX3.Text.Length >= 5)
            {
                pictureBox4.Image = Properties.Resources.fzl;
                ok2 = true;
            }
            else
            {
                pictureBox4.Image = Properties.Resources.fzk;
                ok2 = false;
            }

            //if (textBoxX2.Text.Length >= 5)
            //    {
            //        pictureBox3.Image = Properties.Resources.fzl;
            //        ok1 = true;
            //    }
            //    else
            //    {
            //        pictureBox3.Image = Properties.Resources.fzk;
            //        ok1 = false;
            //    }

                if (ok1 == true && ok2 == true && ok == true)
                {
                    btnChange.Enabled = true;
                }
                else
                {
                    btnChange.Enabled = false;
                }
            
}

        private void btnChange_Click(object sender, EventArgs e)
        {
            if (DialogResult.Yes == MessageBox.Show("是否修改密码！", "提示",MessageBoxButtons.YesNo,MessageBoxIcon.Information))
            {
                if (File.Exists(Application.StartupPath + @"\AdminPassWord.txt"))
                {
                    if (passWord != textBoxX3.Text)
                    {
                        try
                        {
                            using (StreamWriter writer = new StreamWriter(Application.StartupPath + "\\AdminPassWord.txt"))
                            {
                                writer.WriteLine(textBoxX3.Text);
                            }
                            MessageBox.Show("密码修改成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                        catch(Exception err)
                        {
                            MessageBox.Show(err.Message.ToString());
                        }
                    }
                    else
                    {
                        MessageBox.Show("修改密码与原密码相同，请重新修改！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
                textBoxX1.Text = "";
                textBoxX2.Text = "";
                textBoxX3.Text = "";
                pictureBox1.Image = Properties.Resources.fzk;
                pictureBox3.Image = Properties.Resources.fzk;
                pictureBox4.Image = Properties.Resources.fzk;
                ok = false;
                ok1 = false;
                ok2 = false;
            }
        }

        private void btnChange_MouseEnter(object sender, EventArgs e)
        {
            btnChange.BackgroundImage = Properties.Resources.按钮黄色;
        }

        private void btnChange_MouseLeave(object sender, EventArgs e)
        {
            btnChange.BackgroundImage = Properties.Resources.按钮蓝色;
        }

        private void Login_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(Handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
        }

        private void Button3_Click_1(object sender, EventArgs e)
        {
            panelFPW.Visible = true;
        }
    }
}